package com.dongdongshop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brandController")
public class BrandController {

    @Reference
    private BrandService brandService;
    //跳转商品管理
    @RequestMapping("tobrand")
    public String toBrand(Model model){
        //查询商品管理
        List<TbBrand> list = brandService.selectBrandall();
        model.addAttribute("list",list);
        return "admin/brand";
    }

    //商品管理增加
    @RequestMapping("insert")
    @ResponseBody
    public StatusCode insertBrand(TbBrand tbBrand){
        if (tbBrand == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = brandService.insertBrand(tbBrand);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //删除
    @RequestMapping("deleteBrand")
    @ResponseBody
    public StatusCode deleteBrand(String id){
        if (id == null && "".equals(id)){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        String[] ids = id.split(",");
        Boolean b = brandService.deleteBrand(ids);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //回显数据
    @RequestMapping("selectById")
    @ResponseBody
    public StatusCode selectById(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbBrand tbBrand = brandService.selectById(id);
        if (tbBrand == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbBrand);
    }

    //修改商品管理
    @RequestMapping("update")
    @ResponseBody
    public StatusCode updateBrand(TbBrand tbBrand){
        if (tbBrand == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = brandService.updateBrand(tbBrand);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }
        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }



}
